<template>
<div class="order_list_div" style="position: absolute;width: 100%">
	<!--头部-->
	<!-- <header class="aui-bar aui-bar-nav aui-bar-light opacity_title"> -->
	<!-- <router-link to="/main/profile">
		<div class="close_page"  @click="clearSession">
			<i></i>
			<i></i>
			<i></i>
		</div>
	</router-link>
		<div class="aui-title">找回密码</div>
	</header>
	<div style="height:3.75rem;"></div> -->
	<m-header title="找回密码" :hasTitle="true" :hasBack="true"></m-header>
	<div class="aui-content top_div">
		<ul class="aui-list aui-form-list aui-margin-b-15">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">手机号码</div>
					<div class="aui-list-item-input">
						<input type="number" placeholder="请输入手机号码" maxlength="11" v-model="phoneNumber">
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">短信验证码</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="请输入验证码" v-model="mobileCode">
					</div>
					<div class="verify_code">
						<div class="aui-btn" @click="getVerifyCode" v-text="code"></div>
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label">新密码</div>
					<div class="aui-list-item-input">
						<input type="password" placeholder="请输入至少6位的数字或字母" v-model="passWord">
					</div>
				</div>
			</li>
		</ul>
		<div class="aui-content-padded">
			<div class="aui-btn aui-btn-danger aui-btn-block" @click="sendInfo">确认</div>
			<!-- <router-link to="login"> -->
				<div class="aui-btn aui-btn-danger aui-btn-block" @click="clearSession">取消</div>
			<!-- </router-link> -->
		</div>
	</div>
</div>
</template>

<script>
import {changePassWordOne, changePassWordTwo, company} from '../../api/wxh/getData.js'
import Header from "../../real-containers/header"
import {getStore} from '../../api/wxh/session.js'
import {mapGetters, mapMutations} from 'vuex'
import { Toast } from '../../utils/toast.js'
import {showLoading, hideLoading} from '../../utils/loading.js'
import MD5 from '../../utils/md5_wxh.js'

	export default {
		data () {
			return {
				phoneNumber: null,//电话号码
				mobileCode: null,//验证码
				passWord: null,//密码
				code:'获取验证码',
				setInt:null,
				time: 60,
				flag: false,
				getTime:null,
				inputTime: null
			}
		},
		computed: {
			...mapGetters('wxh',[
				'sessionId'
			]),
		    //判断手机号码
		    rightPhoneNumber: function (){
		        return /^1\d{10}$/gi.test(this.phoneNumber)
		    }
		},
		methods: {
			...mapMutations('wxh',[
				'RECORD_SESSIONID',
				'ClEAR_SESSIONID'
			])
			,
			async getVerifyCode(){
				if(this.flag) {
					if(this.time >= 0){
						Toast({
						  message: "60秒后可重新获取！",
						  // iconClass:"aui-iconfont aui-icon-close",
						  duration: 2000,
						  type: 'warn'
						});
						return
					}
				}
				if(this.rightPhoneNumber) {

					//获取短信验证码;
		  let req = await company('eCode='+api.enterpriseCode+'&pNum='+this.phoneNumber);
          let res = await changePassWordOne({
            phone :this.phoneNumber,
            security: MD5(req.msg)
          });

					//判断用户是否存在；
					if(res.returnCode === 0) {
						//弹框提示电话号码不存在；
						Toast({
						  message: res.msg,
						  // iconClass:"aui-iconfont aui-icon-close",
						  duration: 2000,
						  type: 'fail'
						});
						return
					}else if(res.returnCode == '1') {
						//提示输入的手机号尚未绑定；
						//验证码已发送；
						Toast({
						  message: '验证码已发送',
						  // iconClass:"aui-iconfont aui-icon-correct",
						  duration: 2000,
						  type: 'success'
						});

						this.setInt = setInterval(() => {
							if(this.time >= 0) {
								this.code = "重新获取验证码"+"("+this.time+")";
								this.time --;
							}
							if(this.time < 0 ) {
								this.code = "获取验证码";

								this.time = 60;
								this.flag = false;
								clearInterval(this.setInt);
							}
						}, 1000)

						this.flag = true;
					}
					// this.userInfo = res.data[0];
					this.RECORD_SESSIONID(res.data[0])
					// console.log(this.userInfo)

				} else {
					Toast({
					  message: '手机号码输入不正确',
					  // iconClass:"aui-iconfont aui-icon-close",
					  duration: 2000,
					  type: 'fail'
					});
					return
				}


			},
			// stopsetInt() {
			// 	window.clearInterval(this.setInt);
			// 	this.code = "获取验证码";
			// }
			// ,
			async sendInfo() {

				if(!this.rightPhoneNumber) {
					//手机号码输入不正确；
					Toast({
					  message: '手机号码输入不正确',
					  // iconClass:"aui-iconfont aui-icon-close",
					  duration: 2000,
					  type: 'fail'
					});
					return
				} else if(!(/^\d{6}$/gi.test(this.mobileCode))) {
					//验证码不正确
					Toast({
					  message: '验证码不正确',
					  // iconClass:"aui-iconfont aui-icon-close",
					  duration: 2000,
					  type: 'fail'
					});
					return
				} else if(!(/^[\S]{6,}$/.test(this.passWord))) {
					//提示密码不符合格式
					Toast({
					  message: '密码格式不正确',
					  // iconClass:"aui-iconfont aui-icon-close",
					  duration: 2000,
					  type: 'fail'
					});
					return
				}

				let res = await changePassWordTwo(this.phoneNumber, this.mobileCode, MD5(this.passWord), window.api.enterpriseCode);
					showLoading();
				if(res.returnCode === 0) {
					//修改密码失败；
					hideLoading();
					Toast({
					  message: res.msg,
					  iconClass:"aui-iconfont aui-icon-close",
					  duration: 2000,
					  type: 'fail'
					});
				} else
				if(res.returnCode === 1){
					//修改成功返回上一页；
					hideLoading();
					Toast({
					  message: '找回密码成功！',
					  // iconClass:"aui-iconfont aui-icon-correct",
					  duration: 2000,
					  type: 'success'
					});
					this.$router.go(-1);
					return
				} else {
					hideLoading();
						Toast({
						  message: "网络错误",
						  duration: 2000,
						  type: 'fail'
						});

				}


			}
			,
			clearSession () {
				//如果用戶沒有繼續註冊，則在返回時清楚sessionId
				this.ClEAR_SESSIONID('sessionId');
        		this.$router.go(-1);
			}
		},
		watch: {
			userInfo: function(value) {
				this.getVerifyCode();
			}
		},
		components: {
		    "m-header": Header,
		},

	}
</script>
